//
//  ViewControllerForRadioList.m
//  世界
//
//  Created by admin on 16/2/17.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForRadioList.h"
#import "ModelForAll.h"
#import "CellForRadioSubList.h"
#import <UIImageView+WebCache.h>
#import "ViewControllerForRadioPlay.h"
#import <MJRefresh.h>
#import "NetworkingHandler.h"


@interface ViewControllerForRadioList ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tabView;
@property (nonatomic, retain) NSMutableArray *arrPostid;
@property (nonatomic, retain) NSMutableArray *arrTid;
@property (nonatomic, retain) NSMutableArray *arrTname;
@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, retain) NSMutableArray *arrImgsrc;


@end

@implementation ViewControllerForRadioList

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self handleData];
    
    [self createTabView];
    

        self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
                //进入刷新状态后会自动调用这个block
                
                [self handleData];
                
            }];


    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



- (void)handleData
{
    //设置访问地址
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/topicset/ios/radio/%@/0-20.html",self.strForURL];
    
    
    
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        ModelForAll *model = [[ModelForAll alloc] init];
        
        [model setValuesForKeysWithDictionary:result];
        
        self.arrPostid = [NSMutableArray array];
        
        self.arrTid = [NSMutableArray array];
        
        self.arrTname = [NSMutableArray array];
        
        self.arrTitle = [NSMutableArray array];
        
        self.arrImgsrc = [NSMutableArray array];
        
        for (NSDictionary *dic in model.tList)
        {
            NSString *str = [dic objectForKey:@"tid"];
            
            [self.arrTid addObject:str];
            
            NSString *str1 = [dic objectForKey:@"tname"];
            
            [self.arrTname addObject:str1];
            
            NSDictionary *dic2 = [dic objectForKey:@"radio"];
            
            NSString *str2 = [dic2 objectForKey:@"title"];
            
            NSString *str3 = [dic2 objectForKey:@"imgsrc"];
            
            NSString *str4 = [dic2 objectForKey:@"postid"];
            
            
            
            [self.arrTitle addObject:str2];
            
            [self.arrImgsrc addObject:str3];
            
            [self.arrPostid addObject:str4];
            
        }
            
            [self.tabView reloadData];
            
            [self.tabView.mj_header endRefreshing];
        
    }];
}

#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tabView];
    
    self.tabView.dataSource = self;
    
    self.tabView.delegate = self;
    
    [self.tabView registerClass:[CellForRadioSubList class] forCellReuseIdentifier:@"pool1"];
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrTitle.count;
}



#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellForRadioSubList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.lableForTitle.text = self.arrTitle[indexPath.row];
    
    cell.lableForTname.text = self.arrTname[indexPath.row];
    
    [cell.imgViewForImgsrc sd_setImageWithURL:[NSURL URLWithString:self.arrImgsrc[indexPath.row]] placeholderImage:[UIImage imageNamed:@"加载中"]];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}



#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}



#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerForRadioPlay *videoView = [[ViewControllerForRadioPlay alloc] init];
    
    videoView.strForURL = self.arrPostid[indexPath.row];
    
    videoView.number = indexPath.row;
    
    videoView.strForTid = self.arrTid[indexPath.row];
    
      //videoView.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    
    [self presentViewController:videoView animated:YES completion:^{
        
    }];
    
    videoView.lableForTitle.text = self.arrTname[indexPath.row];
}




@end
